package com.yxdj.user.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.yxdj.common.core.domain.R;
import com.yxdj.common.core.utils.poi.ExcelUtil;
import com.yxdj.common.core.web.controller.BaseController;
import com.yxdj.common.core.web.domain.AjaxResult;
import com.yxdj.common.core.web.page.TableDataInfo;
import com.yxdj.common.log.annotation.Log;
import com.yxdj.common.log.enums.BusinessType;
import com.yxdj.common.security.annotation.InnerAuth;
import com.yxdj.common.security.annotation.RequiresPermissions;
import com.yxdj.user.api.domain.User;
import com.yxdj.user.service.IUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 用户信息Controller
 *
 * @author mty
 * @date 2025-03-18
 */
@Tag(name = "用户信息管理")
@RestController
@RequestMapping("/user")
public class UserController extends BaseController
{
    @Autowired
    private IUserService userService;

    /**
     * 查询用户信息列表
     */
    @Operation(summary = "查询用户信息列表")
//    @RequiresPermissions("user:user:list")
    @GetMapping("/list")
    public TableDataInfo list(@Parameter(name = "User" ,description = "User信息") User user)
    {
        startPage();
        List<User> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 导出用户信息列表
     */
    @Operation(summary = "导出用户信息列表")
    @RequiresPermissions("user:user:export")
    @Log(title = "用户信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(@Parameter(name = "response",required = true, description = "response响应对象") HttpServletResponse response, @Parameter(name = "user" ,required = true, description = "User信息") User user)
    {
        List<User> list = userService.selectUserList(user);
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.exportExcel(response, list, "用户信息数据");
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException
    {
        ExcelUtil<User> util = new ExcelUtil<User>(User.class);
        util.importTemplateExcel(response, "用户数据");
    }

    /**
     * 获取用户信息详细信息
     */
    @Operation(summary = "获取用户信息详细信息")
    @RequiresPermissions("user:user:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@Parameter(name = "id" ,required = true, description = "id信息") @PathVariable("id") Long id)
    {
        return success(userService.selectUserById(id));
    }


    /**
     * 修改用户信息
     */
    @Operation(summary = "修改用户信息")
    @RequiresPermissions("user:user:edit")
    @Log(title = "用户信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Parameter(name = "user",required = true, description = "User信息") @RequestBody User user)
    {
        return toAjax(userService.updateUser(user));
    }

    /**
     * 删除用户信息
     */
    @Operation(summary = "删除用户信息")
    @RequiresPermissions("user:user:remove")
    @Log(title = "用户信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@Parameter(name = "ids" ,required = true, description = "主键串") @PathVariable Long[] ids)
    {
        return toAjax(userService.deleteUserByIds(ids));
    }



    @InnerAuth
    @Operation(summary = "供微服务内部远程调用用户登录的接口")
    @PostMapping("/app/login/{phone}")
    public R<User> login(@PathVariable(name = "phone") String phone){
        LambdaQueryWrapper<User> userLambdaQueryWrapper = new LambdaQueryWrapper<>();
        userLambdaQueryWrapper.eq(User::getPhoneNumber, phone);
        User user = userService.getOne(userLambdaQueryWrapper);
        return R.ok(user);
    }


    /**
     * 注册用户信息
     */
    @InnerAuth
    @Operation(summary = "供微服务内部远程调用注册用户的接口")
    @PostMapping("/app/register")
    public R<User> register(@RequestBody User user)
    {
        userService.save(user);
        return R.ok(user);
    }

//    @Operation(summary = "App用户获取用户信息")
//    @GetMapping("/app/getUserInfo")
//    public AjaxResult getUserInfo(){
//        //获取用户信息
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        //获取用户id
//        Long userId = loginUser.getSysUser().getUserId();
//        User user = userService.getById(userId);
//        //创建UserInfoVo对象
//        UserInfoVo userInfoVo = new UserInfoVo(user.getNickname(),user.getAvatarUrl());
//        return AjaxResult.success(userInfoVo);
//    }

    @InnerAuth
    @Operation(summary = "供微服务内部远程调用根据id获取用户信息")
    @GetMapping("/getUserById/{id}")
    public R<User> getUserById(@PathVariable(name = "id") Long id){
        User user = userService.getById(id);
        return R.ok(user);
    }
}
